home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap20 / RndRctMT / RndRctMT.c next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  3.2 KB  |  106 lines

  1. /*------------------------------------------
  2.    RNDRCTMT.C -- Displays Random Rectangles
  3.                  (c) Charles Petzold, 1998
  4.   ------------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include <process.h>
  8.  
  9. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  10.  
  11. HWND hwnd ;
  12. int  cxClient, cyClient ;
  13.  
  14. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  15.                     PSTR szCmdLine, int iCmdShow)
  16. {
  17.      static TCHAR szAppName[] = TEXT ("RndRctMT") ;
  18.      MSG          msg ;
  19.      WNDCLASS     wndclass ;
  20.      
  21.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  22.      wndclass.lpfnWndProc   = WndProc ;
  23.      wndclass.cbClsExtra    = 0 ;
  24.      wndclass.cbWndExtra    = 0 ;
  25.      wndclass.hInstance     = hInstance ;
  26.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  27.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  28.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  29.      wndclass.lpszMenuName  = NULL ;
  30.      wndclass.lpszClassName = szAppName ;
  31.      
  32.      if (!RegisterClass (&wndclass))
  33.      {
  34.           MessageBox (NULL, TEXT ("This program requires Windows NT!"),
  35.                       szAppName, MB_ICONERROR) ;
  36.           return 0 ;
  37.      }
  38.      
  39.      hwnd = CreateWindow (szAppName, TEXT ("Random Rectangles"),
  40.                           WS_OVERLAPPEDWINDOW,
  41.                           CW_USEDEFAULT, CW_USEDEFAULT,
  42.                           CW_USEDEFAULT, CW_USEDEFAULT,
  43.                           NULL, NULL, hInstance, NULL) ;
  44.      
  45.      ShowWindow (hwnd, iCmdShow) ;
  46.      UpdateWindow (hwnd) ;
  47.      
  48.      while (GetMessage (&msg, NULL, 0, 0))
  49.      {
  50.           TranslateMessage (&msg) ;
  51.           DispatchMessage (&msg) ;
  52.      }
  53.      return msg.wParam ;
  54. }
  55.  
  56. VOID Thread (PVOID pvoid)
  57. {
  58.      HBRUSH hBrush ;
  59.      HDC    hdc ;
  60.      int    xLeft, xRight, yTop, yBottom, iRed, iGreen, iBlue ;
  61.      
  62.      while (TRUE)
  63.      {
  64.           if (cxClient != 0 || cyClient != 0)
  65.           {
  66.                xLeft   = rand () % cxClient ;
  67.                xRight  = rand () % cxClient ;
  68.                yTop    = rand () % cyClient ;
  69.                yBottom = rand () % cyClient ;
  70.                iRed    = rand () & 255 ;
  71.                iGreen  = rand () & 255 ;
  72.                iBlue   = rand () & 255 ;
  73.                
  74.                hdc = GetDC (hwnd) ;
  75.                hBrush = CreateSolidBrush (RGB (iRed, iGreen, iBlue)) ;
  76.                SelectObject (hdc, hBrush) ;
  77.                
  78.                Rectangle (hdc, min (xLeft, xRight), min (yTop, yBottom),
  79.                                max (xLeft, xRight), max (yTop, yBottom)) ;
  80.                
  81.                ReleaseDC (hwnd, hdc) ;
  82.                DeleteObject (hBrush) ;
  83.           }
  84.      }
  85. }
  86.  
  87. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  88. {
  89.      switch (message)
  90.      {
  91.      case WM_CREATE:
  92.           _beginthread (Thread, 0, NULL) ;
  93.           return 0 ;
  94.           
  95.      case WM_SIZE:
  96.           cxClient = LOWORD (lParam) ;
  97.           cyClient = HIWORD (lParam) ;
  98.           return 0 ;
  99.           
  100.      case WM_DESTROY:
  101.           PostQuitMessage (0) ;
  102.           return 0 ;
  103.      }
  104.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  105. }
  106.